package qingma

import (
	"qm_system_server/server/global"
	"qm_system_server/server/model/qingma"
    qingmaReq "qm_system_server/server/model/qingma/request"
)

type QmCostService struct {
}

// CreateQmCost 创建qmCost表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmCostService *QmCostService) CreateQmCost(qmCost *qingma.QmCost) (err error) {
	err = global.GVA_DB.Create(qmCost).Error
	return err
}

// DeleteQmCost 删除qmCost表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmCostService *QmCostService)DeleteQmCost(ID string) (err error) {
	err = global.GVA_DB.Delete(&qingma.QmCost{},"id = ?",ID).Error
	return err
}

// DeleteQmCostByIds 批量删除qmCost表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmCostService *QmCostService)DeleteQmCostByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]qingma.QmCost{},"id in ?",IDs).Error
	return err
}

// UpdateQmCost 更新qmCost表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmCostService *QmCostService)UpdateQmCost(qmCost qingma.QmCost) (err error) {
	err = global.GVA_DB.Save(&qmCost).Error
	return err
}

// GetQmCost 根据ID获取qmCost表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmCostService *QmCostService)GetQmCost(ID string) (qmCost qingma.QmCost, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&qmCost).Error
	return
}

// GetQmCostInfoList 分页获取qmCost表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmCostService *QmCostService)GetQmCostInfoList(info qingmaReq.QmCostSearch) (list []qingma.QmCost, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&qingma.QmCost{})
    var qmCosts []qingma.QmCost
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&qmCosts).Error
	return  qmCosts, total, err
}
